package com.wa.enums;

import java.util.HashMap;

/**
 * 日志表枚举
 * @Author Dawn
 * @Date 2021/10/19 9:45
 */
public enum LogTableEnum {

    /** -----操作日志------ */
    COMMON_OPERATION_LOG("t_allchips_common_operation_log", "公共操作日志", LogCategoryEnum.OPERATION.getStatusCode(), LogPlatformEnum.NONE.getStatusCode()),

    /** -----消息日志------ */
    COMMON_MSG_LOG("t_allchips_common_msg_log", "公共消息日志", LogCategoryEnum.MSG.getStatusCode(), LogPlatformEnum.NONE.getStatusCode()),

    /** -----变更日志------ */
    COMMON_CHANGE_LOG("t_allchips_common_change_log", "公共变更日志", LogCategoryEnum.CHANGE.getStatusCode(), LogPlatformEnum.NONE.getStatusCode()),
    USER_CHANGE_LOG("t_allchips_user_change_log", "用户系统变更日志", LogCategoryEnum.CHANGE.getStatusCode(), LogPlatformEnum.USER.getStatusCode()),
    GOODS_CHANGE_LOG("t_allchips_goods_change_log", "商品系统变更日志", LogCategoryEnum.CHANGE.getStatusCode(), LogPlatformEnum.GOODS.getStatusCode()),
    ;

    /**
     * 表名
     */
    private String tableName;
    /**
     * 表描述
     */
    private String tableDesc;
    /**
     * 日志种类
     * {@link LogCategoryEnum}
     */
    private int categoryCode;
    /**
     * 所属平台（0表示为不限平台的公共日志）
     * {@link LogPlatformEnum}
     */
    private int platformCode;

    /**
     * 数据集合（key：categoryCode+"_"+platformCode）
     */
    public static HashMap<String, LogTableEnum> dataMap = new HashMap<>();

    static {
        LogTableEnum[] logTableEnums = LogTableEnum.values();
        for (LogTableEnum logTableEnum : logTableEnums) {
            dataMap.put(logTableEnum.categoryCode+"_"+logTableEnum.platformCode, logTableEnum);
        }
    }

    /**
     * 根据日志种类编号和系统类型编号获取表配置信息
     * @Author Dawn
     * @Date 2021/10/21 17:16
     */
    public static LogTableEnum getLogTable(int categoryCode, int platformCode){
        String key = categoryCode + "_" + platformCode;
        return dataMap.get(key);
    }

    /**
     * 根据日志种类和系统类型获取表配置信息
     * @Author Dawn
     * @Date 2021/10/21 17:03
     */
    public static LogTableEnum getLogTable(LogCategoryEnum logCategoryEnum, LogPlatformEnum logPlatformEnum){
        if (logCategoryEnum == null || logPlatformEnum == null){
            return null;
        }
        return getLogTable(logCategoryEnum.getStatusCode(), logPlatformEnum.getStatusCode());
    }

    /**
     * 根据日志种类编号和系统类型编号获取表配置信息（如果获取的模块没有配置模块专属表则返回公共表）
     * @Author Dawn
     * @Date 2021/10/21 17:16
     */
    public static LogTableEnum getLogTableEnum(int categoryCode, int platformCode){
        String key = categoryCode + "_" + platformCode;
        LogTableEnum logTableEnum = dataMap.get(key);
        if (logTableEnum == null && platformCode > LogPlatformEnum.NONE.getStatusCode()){
            // 如果当前查询的模块未配置系统专属日志表，就分配公共表
            key = categoryCode + "_" + LogPlatformEnum.NONE.getStatusCode();
            logTableEnum = dataMap.get(key);
        }
        return logTableEnum;
    }

    /**
     * 根据日志类型（细分类型：LogTypeEnum）和模块类型获取表配置信息（如果获取的模块没有配置模块专属表则返回公共表）
     * @Author Dawn
     * @Date 2021/10/21 17:09
     */
    public static LogTableEnum getLogTableEnum(LogTypeEnum logTypeEnum, LogModuleEnum logModuleEnum){
        if (logTypeEnum == null || logModuleEnum == null){
            return null;
        }
        LogTableEnum logTableEnum = getLogTable(logTypeEnum.getCategoryCode(), logModuleEnum.getPlatformCode());

        if (logTableEnum == null && logModuleEnum.getPlatformCode() > LogPlatformEnum.NONE.getStatusCode()){
            // 如果当前查询的模块未配置系统专属日志表，就分配公共表
            logTableEnum = getLogTable(logTypeEnum.getCategoryCode(), LogPlatformEnum.NONE.getStatusCode());
        }

        return logTableEnum;
    }

    public String getTableName() {
        return tableName;
    }

    public String getTableDesc() {
        return tableDesc;
    }

    public int getCategoryCode() {
        return categoryCode;
    }

    public int getPlatformCode() {
        return platformCode;
    }

    LogTableEnum(String tableName, String tableDesc, int categoryCode, int platformCode) {
        this.tableName = tableName;
        this.tableDesc = tableDesc;
        this.categoryCode = categoryCode;
        this.platformCode = platformCode;
    }
}
